package com.bjpowernode.mvc.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjpowernode.common.enums.Code;
import com.bjpowernode.db.domain.FinanceAccount;
import com.bjpowernode.db.domain.ProductInfo;
import com.bjpowernode.db.result.BidUserResultMapping;
import com.bjpowernode.mvc.model.bean.PageInfo;
import com.bjpowernode.mvc.model.dto.product.ProductCollDTO;
import com.bjpowernode.mvc.model.dto.product.ProductInfoVO;
import com.bjpowernode.mvc.model.response.Response;
import com.bjpowernode.mvc.model.vo.bid.BidUserVO;
import com.bjpowernode.mvc.model.vo.page.PageInfoVO;
import com.bjpowernode.mvc.model.vo.product.productInfoVO;
import com.bjpowernode.mvc.service.inter.AccountService;
import com.bjpowernode.mvc.service.inter.BidService;
import com.bjpowernode.mvc.service.inter.ProductService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/*@CrossOrigin   //跨域.*/
@RestController
public class ProductController extends BaseController{

    @Resource
    private ProductService productService;
    @Resource
    private BidService bidService;
    @Resource
    private AccountService accountService;
    @GetMapping(value = "/product/show")
    public Response showProducts(){


        //调用service获取理财产品列表
        ProductCollDTO productCollDTO=productService.showProducts();


        //List<ProductInfo>转为List<ProductVO>

        List<ProductInfoVO> xinVOList= BeanUtil.copyToList(productCollDTO.getXinList(), ProductInfoVO.class);
        List<ProductInfoVO> youVOList= BeanUtil.copyToList(productCollDTO.getYouList(), ProductInfoVO.class);
        List<ProductInfoVO> sanVOList = BeanUtil.copyToList(productCollDTO.getSanList(), ProductInfoVO.class);

        Map<String ,Object> data =new HashMap<>();
        data.put("xinList",xinVOList );
        data.put("youList",youVOList );
        data.put("sanList",sanVOList );
        return Response.ok(data);
    }



    //分页查询.
    @GetMapping(value = "/product/type/list")
    public Response queryByType(@RequestParam("type") Integer type,@RequestParam("pageNo") Integer pageNo,@RequestParam("pageSize") Integer pageSize){

        Response response=Response.FAIL();
        if (type!=null &&(type ==1 || type==2)){
            pageNo=defaultPageNo(pageNo);
            pageSize=defaultPageSize(pageSize);
            //按照类型查询数据
           Page<ProductInfo> pageResult= productService.queryPageByType(type,pageNo,pageSize);

           //通过配置page<product> 获取查询的数据和分页
            List<ProductInfo> productList=pageResult.getRecords();
            long current=pageResult.getCurrent();   //pageNo
            long pages=pageResult.getPages();       //页数
            long size=pageResult.getSize();         //pageSize
            long total=pageResult.getTotal();       //总记录数量

            //封装page信息对象
            PageInfo pageInfo=new PageInfo();
            pageInfo.setPageNo(current);
            pageInfo.setPageSize(size);
            pageInfo.setTotalPage(pages);
            pageInfo.setTotalRecord(total);


            //组成返回给前端的数据.
            Map<String,Object> data=new HashMap<>();
            data.put("productList", BeanUtil.copyToList(productList, productInfoVO.class));

            data.put("pageInfo",BeanUtil.copyProperties(pageInfo, PageInfoVO.class));

            response= Response.ok(data);
        }
         return response;
    }




    //产品详情.
    @GetMapping(value = "/product/details")
    public Response queryDetail(@RequestParam Integer id,@RequestHeader(value = "uid",required = false) Integer userId){
        Response response=Response.FAIL(Code.REQUEST_PARAM_ERR);
        if (id!=null &&id>0){
            //查询产品.
            ProductInfo productInfo= productService.queryById(id);

            //查询.
          List<BidUserResultMapping> bidList=  bidService.queryRecentlyBidsByProductId(id,1,5);

          //账号资金.
            BigDecimal accountMoney=new BigDecimal("-1");
            if (userId != null && userId>0){
                //查询资金.
               FinanceAccount account= accountService.queryUserId(userId);
               if (account != null){
                   accountMoney=account.getAvailableMoney();
               }
            }

            //封装结果转vo
            Map<String,Object> data=new HashMap<>();
            data.put("product", BeanUtil.copyProperties(productInfo, productInfoVO.class));
            data.put("bidList", BeanUtil.copyToList(bidList, BidUserVO.class));
            data.put("accountMoney",accountMoney);

            response=Response.ok(data);

        }

        return response;
    }




















}
